package org.qengine.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.qengine.dao.PapersDao;
import org.qengine.entity.PapersEntity;
import org.qengine.entity.QuestionEntity;
import org.qengine.service.PapersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Service("papersService")
public class PapersImpl  extends ServiceImpl<PapersDao, PapersEntity>
        implements PapersService {
    @Autowired
    PapersDao papersDao;
  public   String  savePapers(List<QuestionEntity> questionEntities,Long ruleId,String type){

    Long [] questionIds = new Long[questionEntities.size()];
    for(int i=0;i<questionEntities.size();i++){
      questionIds[i] = questionEntities.get(i).getId().longValue();
    }
    PapersEntity papersEntity = new PapersEntity();
    papersEntity.setQuestionIdsFromArray(questionIds);
    papersEntity.setPaperId(papersDao.getMaxId()+1);
    papersEntity.setRuleId(ruleId);
      // 将 long 类型的时间戳转换为 LocalDateTime
      long currentTimeMillis = System.currentTimeMillis();
      LocalDateTime createTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis), ZoneId.systemDefault());
      papersEntity.setCreateTime(createTime);
      papersEntity.setType(type);
      // 保存试卷
    this.save(papersEntity);
    return "success";
    }
}
